package ar.com.angrymachine.gameui;

import java.awt.Graphics;
import java.awt.Rectangle;

import ar.com.angrymachine.gameui.ScrollBarWidget.ScrollBarListener;

public class ScrollingContainerWidget extends Widget implements ScrollBarListener
{
	private ScrollBarWidget bar;

	public ScrollingContainerWidget(Widget parent, Rectangle area)
	{
		super(parent, area);
		bar = new ScrollBarWidget(this, this, new Rectangle(area.width - GameCanvas.screen.theme.getDefaultBarWidth(), 0, GameCanvas.screen.theme.getDefaultBarWidth(), area.height));
	}
	
	public void setAreaHeight(int newSize)
	{
		bar.setMaximumValue(newSize);
	}
	
	public int getCurrentOffset()
	{
		return bar.getValue();
	}
	
	public void paint(Graphics g)
	{
		if(bar.getPageSize() >= bar.getMaxValue())
		{
			bar.setVisible(false);
		}
		else
		{
			bar.setVisible(true);
		}
	}

	public void onChanged(ScrollBarWidget sender, int oldValue, int newValue)
	{
		int diff = newValue - oldValue;
		// displace children
        for (int i = 0; i < children.size(); i++)
        {
            Widget w = (Widget) children.elementAt(i);
            if(w != bar)
            {
	            Rectangle area = w.getRelativeRect();
	            area.y -= diff;
	            w.setRelativeRect(area);
            }
        }
	}
}
